package com.hckjsec.oa.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.general.ListStringView;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.oa.domain.OaDate;
import com.hckjsec.oa.service.IOaDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hckjsec.oa.mapper.OaConfigMapper;
import com.hckjsec.oa.domain.OaConfig;
import com.hckjsec.oa.service.IOaConfigService;

/**
 * 公司OA配置Service业务层处理
 * 
 * @author cyt
 * @date 2021-12-21
 */
@Service
public class OaConfigServiceImpl implements IOaConfigService 
{
    @Autowired
    private OaConfigMapper oaConfigMapper;
    @Autowired
    private IOaDateService oaDateService;
    @Autowired
    private IOaConfigService oaConfigService;
    /**
     * 查询公司OA配置
     * 
     * @param id 公司OA配置ID
     * @return 公司OA配置
     */
    @Override
    public OaConfig selectOaConfigById(Long id)
    {
        return oaConfigMapper.selectOaConfigById(id);
    }

    /**
     * 查询公司OA配置列表
     * 
     * @param oaConfig 公司OA配置
     * @return 公司OA配置
     */
    @Override
    public List<OaConfig> selectOaConfigList(OaConfig oaConfig)
    {
        return oaConfigMapper.selectOaConfigList(oaConfig);
    }

    /**
     * 新增公司OA配置
     * 
     * @param oaConfig 公司OA配置
     * @return 结果
     */
    @Override
    public int insertOaConfig(OaConfig oaConfig)
    {
        oaConfig.setCreateTime(DateUtils.getNowDate());
        return oaConfigMapper.insertOaConfig(oaConfig);
    }

    /**
     * 修改公司OA配置
     * 
     * @param oaConfig 公司OA配置
     * @return 结果
     */
    @Override
    public int updateOaConfig(OaConfig oaConfig)
    {
        oaConfig.setUpdateTime(DateUtils.getNowDate());
        return oaConfigMapper.updateOaConfig(oaConfig);
    }

    /**
     * 批量删除公司OA配置
     * 
     * @param ids 需要删除的公司OA配置ID
     * @return 结果
     */
    @Override
    public int deleteOaConfigByIds(Long[] ids)
    {
        return oaConfigMapper.deleteOaConfigByIds(ids);
    }

    /**
     * 删除公司OA配置信息
     * 
     * @param id 公司OA配置ID
     * @return 结果
     */
    @Override
    public int deleteOaConfigById(Long id)
    {
        return oaConfigMapper.deleteOaConfigById(id);
    }

    /**
     * 计算时差
     *
     * @param
     * @return 结果
     */
    @Override
    public String getDuration(Date startDate,Date endDate)
    {
        OaConfig oaConfig = new OaConfig();
        List<OaConfig> configList = oaConfigService.selectOaConfigList(null);
        if (!StringUtils.isEmpty(configList)){
            oaConfig = configList.get(0);
        }
        Date calendarStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,"2020-10-10 "+oaConfig.getStartWorkTime()+":00");
        Date calendarEndDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,"2020-10-10 "+oaConfig.getEndWorkTime()+":00");
        Long spacing =(calendarEndDate.getTime() - calendarStartDate.getTime())/1000;//计算正常上下班时长
        String findStartDate = "";
        String findEndDate = "";
        String hcStartDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,startDate);
        String hcEndDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,endDate);
        OaDate oaDate = new  OaDate();
        oaDate.setFestival(0L);
        oaDate.setBeginTime(hcStartDate);
        oaDate.setEndTime(hcEndDate);
        List<OaDate> oaDateList = oaDateService.selectOaDateList(oaDate);
        if (StringUtils.isEmpty(oaDateList)){
            return "error";
        }
        //判断开始日期是否和需上班日期一致
        String searchStartDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,oaDateList.get(0).getDate());
        String searchEndDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,oaDateList.get(oaDateList.size() - 1).getDate());
        if (hcStartDate.equals(searchStartDate)){
            findStartDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,startDate);
        }else{
            findStartDate = searchStartDate+" "+oaConfig.getStartWorkTime()+":00";
        }
        if (hcEndDate.equals(searchEndDate)){
            findEndDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,endDate);
        }else{
            findEndDate = searchEndDate+" "+oaConfig.getEndWorkTime()+":00";
        }
        //1.如果开始和结束在同一天  直接计算时差
        List<ListStringView> findList = new ArrayList<>();
        for (int i = 0; i < oaDateList.size(); i++) {
            ListStringView listStringView = new ListStringView();
            if (i == 0){
                listStringView.setKey(findStartDate);
            }else{
                listStringView.setKey(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,oaDateList.get(i).getDate())+" "+oaConfig.getStartWorkTime()+":00");
            }
            if (i == oaDateList.size() - 1){
                listStringView.setValue(findEndDate);
            }else{
                listStringView.setValue(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,oaDateList.get(i).getDate())+" "+oaConfig.getEndWorkTime()+":00");
            }
            findList.add(listStringView);
        }
        AtomicReference<Long> countInterval = new AtomicReference<>(0L);
        findList.forEach(kv->{
            Date oldDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,kv.getKey());
            Date newDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,kv.getValue());
            long Interval = (newDate.getTime() - oldDate.getTime())/1000;
            if (Interval > 0){
                countInterval.set(countInterval.get() + Interval);
            }
        });
        DecimalFormat df = new DecimalFormat("#.0");
        double aInterval = countInterval.get().doubleValue();
        double bInterval = spacing.doubleValue();
        return Double.valueOf(df.format(aInterval/bInterval)).doubleValue()+"天";
    }
}
